package com.ufo.io.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class NioBasic {
    public static void main(String[] args){
        NioBasic nioBasic = new NioBasic();
//        nioBasic.bufferExamples();
        nioBasic.channelExamples();
    }

    private void channelExamples() {
        fileChannelExample();
    }

    private void fileChannelExample() {
        String basePath="D:/老头环";
        try(FileInputStream fileIn=new FileInputStream(basePath+"/spring-cloud.pdf");
            FileChannel channelIn = fileIn.getChannel();
            FileOutputStream fileOut=new FileOutputStream(basePath+"/spring-cloud2.pdf");
            FileChannel channelOut=fileOut.getChannel();){
            ByteBuffer buf = ByteBuffer.allocate(1024);
            int len=-1;
            while((len=channelIn.read(buf))!=-1){
                System.out.println("read:"+len);
                buf.flip();
                int lenOut=channelOut.write(buf);
                System.out.println("write:"+lenOut);
                buf.clear();
            }
            channelOut.force(true);
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try(RandomAccessFile rFile = new RandomAccessFile(basePath + "/spring-cloud2.pdf","rw");
            FileChannel channel=rFile.getChannel();){

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void bufferExamples() {
        IntBuffer b=IntBuffer.allocate(24);//初始化时开辟内存，在此表示24个int
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());
        b.put(123);
        b.put(0);
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());

        b.flip();//反转之后，进入读模式。position归零
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());

        b.compact();//压缩,再次转为写模式
        //b.clear();//clear清除所有内容
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());

        b.flip();
        //先写先出
        int a1 = b.get();
        System.out.println("get one :"+a1);
        //position增加1
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());
        /* 错误操作：在读模式下写，不会立刻报错，而是在get方法报错
        b.put(22);
        int a2 = b.get();
        System.out.println(a2);*/

        //倒带回放
        b.rewind();
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());
        a1 = b.get();
        System.out.println("get one :"+a1);

        //mark做一个position的保存点， reset可以回到mark的位置
        System.out.println("mark");
        b.mark();
        b.get();
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());
        b.reset();
        System.out.println("reset");
        System.out.println("buffer position="+b.position()+", limit="+b.limit()+", capacity="+b.capacity());


    }
}
